<?php
namespace app\admin\controller;
use think\Db;
use think\facade\{
    Config,Request,Cache,App
};
class Userbill extends Base
{
    //列表
    public function lists(){
      if(is_post()){
          $d = $this->post;
          $map = [];
          if($d['keyword']) $map[] = ['and',"b.username|a.order_no","like","%{$d['keyword']}%"];
          if($d['type']) $map[] = ['and',"a.type","=",$d['type']];
          if($d['min_time']) $map[] = ['and','a.create_time','>=',strtotime($d['min_time'])];
          if($d['max_time']) $map[] = ['and','a.create_time','<=',strtotime($d['max_time'])];
          $w  = where($map);
          $sql = "select a.*,b.username from user_bill a left join user b on a.user_id=b.id  $w[0] order by a.id desc".limit($d['page'],$d['limit']);
          $rs = $this->db->query($sql, $w[1],'',true);
          if($rs){
              $rule = [
                  'type' => function($v){return c("type.user_bill_type.{$v}");},
                  'create_time' => 'com_time_format',
              ];
              $data = com_format($rs[0],$rule);
              return suc($data, $rs[1]);
          }else{
              suc([],0);
          }
      }
      $this->fetch();
    }
    //编辑
    public function del(){
      if(is_post()){
        $id = intval($this->post["id"]);
        $rs = $this->db->delete("user_bill",$id);
        if($rs){
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }else{
            return err("删除失败");
        }
      }

    }
}
